package cn.fbs.interceptor;

import cn.fbs.dao.dm.SysUserMapper;
import cn.fbs.model.dm.SysUser;
import cn.fbs.util.AESUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Base64;

/**
 * @program: springbootdemo
 * @description: 拦截器配置类
 * @author: wxk
 * @create: 2020-12-21 08:48
 **/
@Slf4j
@Component
public class AuthorityInterceptor implements HandlerInterceptor {

    @Autowired
    private SysUserMapper userMapper;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        String requestURI = request.getRequestURI();
        if (requestURI.contains("/img")){
            return true;
        }
        SysUser sysUser = null;
        try {
//            request.getre
            String sessionId = request.getHeader("sessionId");
            if (sessionId == null) {
                response.setHeader("Content-type", "text/html;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("333,请传sessionId");
                return false;
            }
            sessionId = new String(Base64.getDecoder().decode(sessionId));
            JSONObject jsonObject = JSONObject.parseObject(sessionId);
            String loginName = jsonObject.get("loginName").toString();
            String password = AESUtils.decryptData(jsonObject.get("password").toString());
            sysUser = userMapper.selectByLoginName(loginName);
            if (sysUser==null){
                response.setHeader("Content-type", "text/html;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("333,用户名不存在");
            }
            String myPassword = sysUser.getPassword();
            if (!password.equals(myPassword)) {
                response.setHeader("Content-type", "text/html;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("333,密码错误");
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("333,登录失败");
            return false;
        }
        HttpSession session = request.getSession();
        session.setAttribute("userId", sysUser.getUserId());
        session.setAttribute("userName",sysUser.getUserName());
        session.setAttribute("loginName",sysUser.getLoginName());
        session.setAttribute("userRole",sysUser.getUserRole());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        log.debug("postHandle method is running");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        log.debug("afterCompletion method is running");
    }
}
